#!/bin/sh
# Time-stamp: <05/08/09 14:41:27 ostolop>

# Initial data creation and loading script. Not tested on Oracle!

USAGE="Usage: init_data.sh ORACLE|POSTGRES DB|SID HOST USER"

if [ -z $1 ]
then
    echo $USAGE
    exit;
fi

PLATFORM=$1
DB=$2
HOST=$3
USER=$4

if [ "$HOST" ]
then
    HOST="-h $HOST";
fi

if [ -z "$PLATFORM" -o -z "$DB" ]
then
    echo "Must specify both arguments, platfrom (ORACLE or POSTGRES) and the database identifier (SID, if using Oracle)."
    echo
    echo $USAGE
    exit;
fi 

if [ $PLATFORM = "POSTGRES" ]
then
    echo "Initializing a PostgreSQL database..."

    psql $DB $HOST $USER -q --file DB-DDL-postgres.sql 2>/dev/null
    psql $DB $HOST $USER -q --file init_data.sql
    psql $DB $HOST $USER -q --command "\copy ep_error from '`pwd`/errors.csv'"
    psql $DB $HOST $USER -q --command "\copy ep_component from '`pwd`/components.csv'"

    echo "Done."

elif [ $PLATFORM = "ORACLE" ]
then
    echo "Initializing an Oracle database..."

    sqlplus $DB @DB-DDL-oracle.sql
    sqlplus $DB @init_data.sql
    sqlldr $DB control=load-errors-oracle.ctl
    sqlldr $DB control=load-components-oracle.ctl

    echo "Done."
fi

